import React from 'react';
import cx from 'classnames';
import {findDOMNode} from "react-dom";
import { DatePicker } from 'antd';

export default React.createClass({

    propTypes: {
        label: React.PropTypes.string.isRequired,
        isRequired: React.PropTypes.bool,
        name: React.PropTypes.string,
        onChange: React.PropTypes.func,
        defaultValue: React.PropTypes.string,
    },

    getInitialState: function() {
        return {
            errMsg: null,
        };
    },

    componentWillReceiveProps: function(nextProps) {
        if(nextProps.defaultValue !== null && nextProps.defaultValue !== undefined){
            this.setState({dateString: nextProps.defaultValue});
        }
    },

    render: function() {
        return (
            <div>
                <div className="item-input jrh-time">
                    <span className="input-label">{this.props.label}</span>
                    <DatePicker value={this.state.value} onChange={this.change} placeholder="选择" />
                </div>
            </div>
        )
    },

    change: function(date, dateString){
        this.setState({value: date, dateString: dateString});
        let value = dateString;
        if(this.props.onChange && typeof this.props.onChange === "function"){
            if (this.props.name.indexOf('filed' > -1)) {
                this.props.onChange(value, this.props.name);
            }else {
                this.props.onChange(value);
            }
        };
    },

    getValue: function() {
        // const value = this.state.value;
        // return value;
        const dateString = this.state.dateString;
        return dateString;
    }
})
